package com.winhxd.b2c.order.adapter.impl;

import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.domain.order.condition.OrderStartAfterSaleCondition;
import com.winhxd.b2c.common.domain.order.enums.AfterSaleTypeEnum;
import com.winhxd.b2c.common.domain.order.model.OrderInfo;
import com.winhxd.b2c.common.exception.BusinessException;
import com.winhxd.b2c.order.adapter.AbstractAfterSaleAdapter;
import com.winhxd.b2c.order.service.OrderService;
import com.winhxd.b2c.order.util.OrderUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;

/**
 * 待发货退款申请撤销
 * @author hjw
 * @create 2019-01-09 14:36
 **/
public class AfterSaleCancel extends AbstractAfterSaleAdapter {

    private static final Logger LOGGER = LoggerFactory.getLogger(AfterSaleCancel.class);

    @Resource
    public OrderService orderService;

    @Override
    public boolean execute(OrderStartAfterSaleCondition condition) {
        //除待发货-取消退款之外都需要校验是否已到自动收货时间
        if(condition.getAfterSaleType() != AfterSaleTypeEnum.WAIT_DELIVERING_CANCEL_REFUND.getTypeCode()){
            //查询订单信息
           OrderInfo oInfo = orderService.selectByOrderNo(condition.getOrderNo());
           if(OrderUtils.isAutoRecive(oInfo.getCreated(),oInfo.getAutoCloseMinute().intValue())){
               LOGGER.error("订单{}已自动确认收货，不能在进行任何操作！！",condition.getOrderNo());
                throw new BusinessException(BusinessCode.CODE_440601);
           }
        }
        orderService.orderRevokeAfterSale(condition);
        return true;
    }
}
